/**
 * 模拟死锁的实现过程
 * @Author：徐志荣
 * @Date：2023/8/27 上午 10:29
 */


public class Day0827 {

    static Object lockA = new Object();
    static Object lockB = new Object();

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread() + "尝试获取锁A");
                synchronized (lockA){
                    System.out.println(Thread.currentThread() + "拿到锁A");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread() + "尝试获取锁B");
                    synchronized (lockB){
                        System.out.println(Thread.currentThread() + "拿到锁B");
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread() + "尝试获取锁B");
                synchronized (lockB){
                    System.out.println(Thread.currentThread() + "拿到锁B");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread() + "尝试获取锁A");
                    synchronized (lockA){
                        System.out.println(Thread.currentThread() + "拿到锁A");
                    }
                }
            }
        }).start();

    }
}
